package com.howie.parnote.service.impl;


import com.howie.parnote.constant.DbNumConstant;
import com.howie.parnote.dao.*;
import com.howie.parnote.exception.ApplicationProcessException;
import com.howie.parnote.module.NoteCommentModule;
import com.howie.parnote.module.NoteFeedModule;
import com.howie.parnote.module.NoteModule;
import com.howie.parnote.module.SharerModule;
import com.howie.parnote.service.NoteService;
import com.howie.parnote.service.common.ApplicationDefaultServiceImpl;
import com.howie.parnote.util.DateUtil;
import com.howie.parnote.validator.bean.note.FormNoteBean;
import com.howie.parnote.validator.bean.note.ServiceNoteBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2014/12/18 0018.
 * @version v1.0.0
 */
@Service
public class NoteServiceImpl extends ApplicationDefaultServiceImpl<NoteModule> implements NoteService {

	private byte[] lock = new byte[0];

	@Override
	public void insertNote(FormNoteBean formBean, SharerModule sharer) {
		if (formBean.getServiceNoteBean() != null) {
			ServiceNoteBean serviceBean = formBean.getServiceNoteBean();
			NoteModule note = getNoteModule(sharer);
			note.setShareContent(formBean.getContent());
			if (!serviceBean.isCustom()) {
				note.setRole(serviceBean.getRoleModule());
				note.setLocation(serviceBean.getLocationModule());
				note.setIncident(serviceBean.getIncidentModule());
			} else {
				if (serviceBean.isCustomRole()) {
					note.setCustomRole(formBean.getRoleName());
				} else {
					note.setRole(serviceBean.getRoleModule());
				}
				if (serviceBean.isCustomLocation()) {
					note.setCustomLocation(formBean.getLocationName());
				} else {
					note.setLocation(serviceBean.getLocationModule());
				}
				if (serviceBean.isCustomIncident()) {
					note.setCustomIncident(formBean.getIncidentName());
				} else {
					note.setIncident(serviceBean.getIncidentModule());
				}
			}
			insertModuleSelective(note);
		}
	}

	private NoteModule getNoteModule(SharerModule sharer) {
		NoteModule note = new NoteModule();
		note.setSharer(sharer);
		note.setFascinating(DbNumConstant.DEFAULT_VALUE.getValue().intValue());
		note.setBored(DbNumConstant.DEFAULT_VALUE.getValue().intValue());
		note.setShareTime(DateUtil.getTimestamp());
		note.setUseStatus(DbNumConstant.USING.getValue().byteValue());
		return note;
	}

	@Override
	public void insertNoteFeed(Integer noteId, String feedType, SharerModule sharer) throws ApplicationProcessException {
		Assert.notNull(noteId);
		Assert.notNull(feedType);
		NoteFeedModule feedExist;
		feedExist = noteFeedModuleMapper.selectExist(sharer.getSharerId(), noteId);
		if (feedExist != null) {
			throw new ApplicationProcessException();
		}
		synchronized (lock) {
			NoteModule todo = noteModuleMapper.selectByPrimaryKey(noteId);
			Assert.notNull(todo);

			NoteFeedModule feed = new NoteFeedModule();
			feed.setSharer(sharer);
			feed.setFeedTime(DateUtil.getTimestamp());
			feed.setNoteId(noteId);
			if (feedType.equals(NoteFeedModule.FeedType.FEED_TYPE_LIKE.getValue())) {
				feed.setFeedType(NoteFeedModule.FeedType.FEED_TYPE_LIKE.getValue());
				todo.setFascinating(todo.getFascinating() + 1);
			} else if (feedType.equals(NoteFeedModule.FeedType.FEED_TYPE_UN_LIKE.getValue())) {
				feed.setFeedType(NoteFeedModule.FeedType.FEED_TYPE_UN_LIKE.getValue());
				todo.setBored(todo.getBored() + 1);
			} else {
				throw new IllegalArgumentException();
			}

			updateByPrimaryKeySelective(todo);
			noteFeedModuleMapper.insert(feed);
		}
	}

	@Override
	public void insertComment(Integer noteId, String content, SharerModule sharerModule) {
		NoteModule noteModule = noteModuleMapper.selectByPrimaryKey(noteId);
		NoteCommentModule commentModule = new NoteCommentModule();
		commentModule.setNoteId(noteId);
		commentModule.setSharer(sharerModule);
		commentModule.setCommentContent(content);
		commentModule.setCommentTime(DateUtil.getTimestamp());
		commentModuleMapper.insert(commentModule);
	}

	@Autowired
	private RoleModuleMapper roleModuleMapper;
	@Autowired
	private LocationModuleMapper locationModuleMapper;
	@Autowired
	private IncidentModuleMapper incidentModuleMapper;
	@Autowired
	private NoteModuleMapper noteModuleMapper;
	@Autowired
	private NoteFeedModuleMapper noteFeedModuleMapper;
	@Autowired
	private NoteCommentModuleMapper commentModuleMapper;
}
